// pages/blog/blog.
let user_id = ''
let keyword = ''
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 控制底部弹出层是否显示
    modalShow: false,
    blogList: '',
  },

  //发布功能
  onPublish() {
    wx.getSetting({
      success: (res) => {
        console.log(res)
        if (res.authSetting['scope.userInfo']) {
          wx.getUserInfo({
            success: (res) => {
              console.log(res)
              this.onLoginSuccess({
                detail: res.userInfo
              })
            }
          })
        } else {
          this.setData({
            modalShow: true,
          })
        }
      }
    })
  },

  onLoginSuccess(event) {
    console.log(event)
    const detail = event.detail

    wx.navigateTo({
      url: `../blog-edit/blog-edit?nickName=${detail.nickName}&avatarUrl=${detail.avatarUrl}`,
    })
  },
  onLoginFail() {
    wx.showModal({
      title: '发布需授权',
      content: '当前未授权',
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getUserId()
    this._loadBlogList()
  },

  onSearch(event) {
    
    keyword = event.detail.keyword
    // this._loadBlogList(0)
    if(keyword.trim() != ''){
      console.log(event.detail.keyword)
      this.setData({
        blogList: []
      })
      wx.request({
        url: 'http://127.0.0.1:8000/blog_search_into/',
        method: 'POST',
        data: {
          keyword
        },
        success: ((res) =>{
          console.log(res)
          let blog_list = []
          let blog_data = res.data.blog_info
          let blog_info_image = res.data.blog_info_image
          for (let i = 0, len = blog_data.length; i < len; i++) {
            blog_list.push(blog_data[i])
          }
          // console.log(blog_list)
          // console.log(blog_list[0].id)
          for (var i in blog_list) {
            // console.log(blog_list[i].id)
            for (var item in blog_info_image) {
              // console.log(item)
              if (blog_list[i].id == item) {
                blog_list[i]['images'] = blog_info_image[item]
                this.setData({
                  blogList: blog_list
                })
              }
            }
          }
          console.log(this.data.blogList)
        })
      })
    } else {
      wx.showModal({
        title: '查询内容不能为空',
        content: '请输入内容',
      })
      return
    }

    
  },


  getUserId() {
    wx.getStorage({
      key: 'user_id',
      success: (res) => {
        user_id = res.data
        this._loadBlogList()
      },
      fail: (err) => {
        wx.showToast({
          title: '未登录',
          icon: '',
          image: '',
          duration: 0,
          mask: true,
          success: function(res) {},
          fail: function(res) {},
          complete: function(res) {},
        })
      }
    })
  },

  _loadBlogList() {
    console.log(keyword)
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: 'http://127.0.01:8000/blog_search/',
      method: 'POST',
      data: {
        // user_id: user_id,
        keyword,
      },
      success: ((res) => {
        console.log(res)
        let blog_list = []
        let blog_data = res.data.blog_info
        let blog_info_image = res.data.blog_info_image
        for (let i = 0, len = blog_data.length; i < len; i++) {
          blog_list.push(blog_data[i])
        }
        // console.log(blog_list)
        // console.log(blog_list[0].id)
        for (var i in blog_list) {
          // console.log(blog_list[i].id)
          for (var item in blog_info_image) {
            // console.log(item)
            if (blog_list[i].id == item){
              blog_list[i]['images'] = blog_info_image[item]
              this.setData({
                blogList: blog_list
              })
            }
          }
        }
        console.log(this.data.blogList)
        wx.hideLoading()
        wx.stopPullDownRefresh()
      })
    })
    // wx.showLoading({
    //   title: '加载中...',
    // })
    // wx.cloud.callFunction({
    //   name: 'blog',
    //   data: {
    //     keyword,
    //     start,
    //     count: 10,
    //     $url: 'list',
    //   }
    // }).then((res) => {
    //   this.setData({
    //     blogList: this.data.blogList.concat(res.result)
    //   })
    //   wx.hideLoading()
    //   wx.stopPullDownRefresh()
    // })
  },

  goComment(event) {
    wx.navigateTo({
      url: '../../pages/blog-comment/blog-comment?blogId=' + event.target.dataset.blogid,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.setData({
      blog_list: []
    })
    this._loadBlogList()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})